import dayjs from "dayjs"

const myStorage = {}

function formatKey(k) {
  if (k === 'token') {
    return window.$cfgs.tkkey + "." + k
  }
  return window.$cfgs.appkey + "." + k
}

myStorage.remove = function(k) {
    k = formatKey(k)
    localStorage.removeItem(k)
}

myStorage.put = function(k, jsd) {
    k = formatKey(k)
    if (!jsd) {
      return localStorage.removeItem(k)
    }
    const obj = {
      val: jsd,
      timestamp: dayjs().format() 
    }
    localStorage.setItem(k, JSON.stringify(obj))
}

myStorage.set = myStorage.put

myStorage.get = function(k) {
  k = formatKey(k)
  var item = localStorage.getItem(k) || '{}'
  const obj = JSON.parse(item)
  return obj.val
}

export default myStorage